package jmine.tec.web.front.services.incluir;

import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;
import jmine.tec.web.front.domain.BackOfficeRequestStatus;

/**
 * Serviço de inclusão de um status de request ao backoffice.
 * 
 * @author wagner.tsuchiya
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class BackOfficeRequestStatusService implements ServiceFiller<BackOfficeRequestStatus> {

    public static final String DATABASE_ID = "Database ID";

    public static final String NAME_FIELD = "Nome";

    private PackEntityPersistenceHelper helper;

    private Long databaseId;

    private String name;

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }

    /**
     * @param databaseId the databaseId to set
     */
    @Input(fieldName = DATABASE_ID, required = true)
    public void setDatabaseId(Long databaseId) {
        this.databaseId = databaseId;
    }

    /**
     * @param name the name to set
     */
    @Input(fieldName = NAME_FIELD, required = true)
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @throws PackException exceção na hora de salvar a mensagem.
     */
    @Execution
    public void execute() throws PackException {
        BackOfficeRequestStatus status = this.helper.findOrCreate(BackOfficeRequestStatus.class, this.databaseId, this.name);
        status.setName(this.name);
        this.helper.save(status, this.databaseId);
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, BackOfficeRequestStatus entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
        ExportUtils.put(bean, NAME_FIELD, entity.getName());
    }
}
